package cn.akwangl.common.utils;

import cn.akwangl.common.HttpConstant;
import lombok.Data;

import javax.servlet.http.HttpServletRequest;

/**
 * Http 工具类
 *
 * @author youyongkun
 * @date 2022/5/16 20:37
 */
@Deprecated
public class HttpUtils {

    /**
     * 获取客户端ip地址
     *
     * @param request servlet请求实体
     * @return 客户端IP地址
     * @since 0.0.2
     */
    public static String getClientIp(HttpServletRequest request) {
        String ip = request.getHeader(HttpConstant.X_FORWARDED_FOR);
        if (ip == null || ip.trim() == "" || HttpConstant.UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader(HttpConstant.PROXY_CLIENT_IP);
        }
        if (ip == null || ip.trim() == "" || HttpConstant.UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader(HttpConstant.WL_PROXY_CLIENT_IP);
        }
        if (ip == null || ip.trim() == "" || HttpConstant.UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        // 多个路由时，取第一个非unknown的ip
        final String[] arr = ip.split(",");
        for (final String str : arr) {
            if (!HttpConstant.UNKNOWN.equalsIgnoreCase(str)) {
                ip = str;
                break;
            }
        }
        return ip;
    }
}